//
//  MDMyCarViewController.m
//  OneCreditTreasure
//
//  Created by iOS Tedu on 16/8/30.
//
//

#import "MDMyCarViewController.h"
#import "MDMyCarInfoTableViewCell.h"
#import "CarInfoViewController.h"
#import "myCarModel.h"

@interface MDMyCarViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UIButton *addNewCarBtn;
@property (nonatomic, strong) UIBarButtonItem *rightBtn;

@property (nonatomic, strong) NSMutableArray *selectedModels;
@end

@implementation MDMyCarViewController
#pragma mark - UITableView dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.carModels.count;
}

kRemobeCellSeparator
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MDMyCarInfoTableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
    myCarModel *model = self.carModels[indexPath.row];
    Cell.carNameLB.text = [NSString stringWithFormat:@"%@", model.carName];
    Cell.carPlateNumLB.text = [NSString stringWithFormat:@"车牌号: %@", model.plateNum];
    Cell.carFrameNumLB.text = [NSString stringWithFormat:@"车架号: %@", model.carFrameNum];
    Cell.carEngineNumLB.text = [NSString stringWithFormat:@"发动机号: %@", model.engineNum];
    
    return Cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}

#pragma mark - UITableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView.isEditing) {
        [self.selectedModels addObject:self.carModels[indexPath.row]];
        self.rightBtn.title = self.selectedModels.count == 0 ? @"取消" : @"删除";
    } else {
        CarInfoViewController *vc = [CarInfoViewController new];
        vc.model = self.carModels[indexPath.row];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView.isEditing) {
        [self.selectedModels removeObject:self.carModels[indexPath.row]];
        self.rightBtn.title = self.selectedModels.count == 0 ? @"取消" : @"删除";
    }
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

#pragma mark - 方法 methods
- (void)manageMyCar:(UIBarButtonItem *)sender {
    if (!self.tableView.isEditing) {
        self.tableView.editing = !self.tableView.isEditing;
        self.rightBtn.title = self.selectedModels.count == 0 ? @"取消" : @"删除";
    } else {
        self.rightBtn.title = @"管理";
        if (self.selectedModels.count != 0) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"删除选中项" message:@"删除后不可恢复,您确认要删除吗?" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [self.carModels removeObjectsInArray:self.selectedModels];
                [self.selectedModels removeAllObjects];
                [self.tableView reloadData];
                self.tableView.editing = !self.tableView.isEditing;
            }];
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                [self.selectedModels removeAllObjects];
                self.tableView.editing = !self.tableView.isEditing;
            }];
            [alert addAction:confirm];
            [alert addAction:cancel];
            [self presentViewController:alert animated:YES completion:nil];
        } else {
            self.tableView.editing = NO;
        }
    }
}

- (void)addNewCar:sender {
    CarInfoViewController *vc = [CarInfoViewController new];
    vc.modify = YES;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 生命周期 life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (instancetype)init {
    if (self = [super init]) {
        self.title = @"我的车辆";
        [self tableView];
        [self addNewCarBtn];
        _rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"管理" style:UIBarButtonItemStylePlain target:self action:@selector(manageMyCar:)];
        self.navigationItem.rightBarButtonItem = _rightBtn;
    }
    return self;
}

#pragma mark - 懒加载 lazy loading
- (UITableView *)tableView {
    if(_tableView == nil) {
        _tableView = [[UITableView alloc] init];
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(0);
            make.bottom.equalTo(self.addNewCarBtn.mas_top).equalTo(0);
        }];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        [_tableView registerClass:[MDMyCarInfoTableViewCell class] forCellReuseIdentifier:@"Cell"];
        
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

- (NSMutableArray *)carModels {
    if(_carModels == nil) {
        _carModels = [[myCarModel demoData] mutableCopy];
    }
    return _carModels;
}

- (UIButton *)addNewCarBtn {
    if(_addNewCarBtn == nil) {
        _addNewCarBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.view addSubview:_addNewCarBtn];
        [_addNewCarBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(0);
            make.height.equalTo(44);
        }];
        [_addNewCarBtn setTitle:@"+添加车辆" forState:UIControlStateNormal];
        _addNewCarBtn.backgroundColor = kRGBA(0, 0, 0, 0.8);
        [_addNewCarBtn addTarget:self action:@selector(addNewCar:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addNewCarBtn;
}

- (NSMutableArray *)selectedModels {
    if(_selectedModels == nil) {
        _selectedModels = [[NSMutableArray alloc] init];
    }
    return _selectedModels;
}

@end
